package com.utf999.qiangmanger.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.utf999.qiangmanger.dao.iface.ICompanyDAO;
import com.utf999.qiangmanger.entity.orm.AdminUserEntity;
import com.utf999.qiangmanger.entity.orm.CompanyEntity;
import com.utf999.qiangmanger.entity.orm.StorefrontEntity;
import com.utf999.qiangmanger.exception.BizException;

import com.utf999.qiangmanger.service.AbstractService;
import com.utf999.qiangmanger.service.iface.IAdminUserService;
import com.utf999.qiangmanger.service.iface.ICompanyService;
import com.utf999.qiangmanger.service.iface.IOrderTypeService;
import com.utf999.qiangmanger.service.iface.IStoreUserGroupService;
import com.utf999.qiangmanger.service.iface.IStorefrontService;

/**
 * @author yinwenjie
 */
@Component("CompanyServiceImpl")
public class CompanyServiceImpl extends AbstractService<CompanyEntity, ICompanyDAO> implements ICompanyService {
	@Autowired
	private ICompanyDAO companyDAO;
	
	@Autowired
	private IAdminUserService adminUserService;
	
	@Autowired
	private IStorefrontService storefrontService;
	
	@Autowired
	private IStoreUserGroupService storeUserGroupService;
	
	@Autowired
	private IOrderTypeService orderTypeService;
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.AbstractService#getBaseDAO()
	 */
	@Override
	protected ICompanyDAO getBaseDAO() {
		return this.companyDAO;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICompanyService#queryCompanyList()
	 */
	@Override
	public List<CompanyEntity> queryCompanyList(Integer status) throws BizException {
		return this.companyDAO.queryCompanyList(status);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICompanyService#queryCompany(java.lang.String)
	 */
	@Override
	public CompanyEntity queryCompany(String userid) throws BizException {
		if(StringUtils.isEmpty(userid)) {
			return null;
		}
		
		/*
		 * 偷懒的方式，使用queryCompanyList后，筛选出id相同的一条记录进行翻译
		 * */
		List<CompanyEntity> companys = this.queryCompanyList(null);
		AdminUserEntity adminUser = (AdminUserEntity)this.adminUserService.getEntity(userid);
		
		//如果条件成立，说明没有用户信息
		if(adminUser == null) {
			return null;
		}
		
		CompanyEntity adminCompany = adminUser.getCompany();
		for (CompanyEntity company : companys) {
			if(StringUtils.equals(adminCompany.getUid(), company.getUid())) {
				return company;
			}
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICompanyService#queryCompanyAndStoreList()
	 */
	@Override
	public LinkedHashMap<CompanyEntity, List<StorefrontEntity>> queryCompanyAndStoreList() throws BizException {
		List<CompanyEntity> companys = null;
		companys = this.companyDAO.queryCompanyList(1);
		
		/*
		 * 在得到了 公司列表后，在依次进行店面列表的查询
		 * */
		LinkedHashMap<CompanyEntity, List<StorefrontEntity>> companysList = new LinkedHashMap<CompanyEntity, List<StorefrontEntity>>();
		for (CompanyEntity company : companys) {
			//只查询状态正确的店面
			List<StorefrontEntity> stores = this.storefrontService.queryStorefrontList(company.getUid() , 1);
			companysList.put(company, stores);
		}
		
		return companysList;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICompanyService#queryCompanyAndStoreList(java.lang.String)
	 */
	@Override
	public LinkedHashMap<CompanyEntity, List<StorefrontEntity>> queryCompanyAndStoreList(String userid) throws BizException {
		if(StringUtils.isEmpty(userid)) {
			return null;
		}
		
		AdminUserEntity adminUser = (AdminUserEntity)this.adminUserService.getEntity(userid);
		CompanyEntity company = adminUser.getCompany();
		//这样写是为了hibernate能够查询
		String companyid = company.getUid();
		LinkedHashMap<CompanyEntity, List<StorefrontEntity>> companysList = new LinkedHashMap<CompanyEntity, List<StorefrontEntity>>();
		//如果条件成立说明查询这个公司下所有的店面信息，否则只查询这个管理员所在店面的信息
		if(adminUser.getAdminLevel() == 2) {
			List<StorefrontEntity> stores =  this.storefrontService.queryStorefrontList(companyid, null);
			companysList.put(company, stores);
		} else {
			StorefrontEntity storefront = adminUser.getStorefront();
			storefront.getUid();
			
			//写入结构
			List<StorefrontEntity> stores = new ArrayList<StorefrontEntity>();
			stores.add(storefront);
			companysList.put(company, stores);
		}
		
		
		
		
		
		return companysList;
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICompanyService#queryCompanyByuserid(java.lang.String)
	 */
	@Override
	public CompanyEntity queryCompanyByuserid(String adminuserid) throws BizException {
		if(StringUtils.isEmpty(adminuserid)) {
			return null;
		}
		
		return this.companyDAO.queryCompanyByuserid(adminuserid);
	}
	
	
	@Override
	public void saveCompany(CommonsMultipartFile companyURLFile, String uid,
			String createTime, String status, String companyURL,
			String companyName, String industryType) throws BizException {
		/*
		 * 创建公司的过程是：
		 * 1、首先建立公司基本信息
		 * 2、然后是可能上传的Log标记
		 * 3、初始化这个公司默认的系统级“用户组”信息
		 * 4、初始化这个公司默认的系统级“订单类型”信息
		 * */
		//1、=======首先构造对象
		CompanyEntity company = new CompanyEntity();
		company.setUid(uid);
		company.setCompanyName(companyName);
		company.setCompanyURL(companyURL);
		//时间
		if(!StringUtils.isEmpty(createTime)) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				company.setCreateTime(dateFormat.parse(createTime));
			} catch (ParseException e) {
				company.setCreateTime(new Date());
			}
		} else {
			company.setCreateTime(new Date());
		}
		//公司性质
		if(industryType != null) {
			company.setIndustryType(Integer.parseInt(industryType));
		}
		company.setStatus(Integer.parseInt(status));
		
		//2、=======然后处理可能进行了上传的文件信息
		if(companyURLFile != null) {
			
		}
		//=======根据uid参数，决定是添加还是修改
		//如果条件成立，说明是添加操作；否则是删除操作
		if(StringUtils.isEmpty(uid)) {
			this.insert(company);
			//3、==============
			this.orderTypeService.initDefaultOrderType(company);
			//4、==============
			this.storeUserGroupService.initDefaultStoreUserGroup(company);
		} else {
			this.update(company);
		}
	}
}
